package pbbltobpel;

import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import pbbltobpel.parser.PddlPlan;

import com.predic8.wsdl.WSDLParser;

public class program {

	static String stripExtension (String str) {
        if (str == null) 
        	return null;
        int pos = Math.max(str.lastIndexOf("\\"), str.lastIndexOf("/"));
        if(pos > 0)
        	str = str.substring(pos + 1);
        pos = str.lastIndexOf(".");
        if (pos == -1) 
        	return str;
        return str.substring(0, pos);
    }


	public static PddlPlan Load(String fileName, Object[] wsdls) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse (new File(fileName));
        Element el = doc.getDocumentElement();
     
        WSDLParser parser = new WSDLParser();
        
        PddlPlan plan = new PddlPlan();
        plan.getFromXmlElement(el);
        plan.setFileName(stripExtension(fileName));
        for(Object wsdl : wsdls)
        	plan.getWsdls().put((String)wsdl, parser.parse("file:///" + ((String)wsdl).replace("\\", "/")));
        return plan;
	}
	
	private static void createAndShowGUI() {
		JFrame frame = new JFrame("PDDL2BPEL");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.add(new Gui());
		
		frame.pack();
		frame.setVisible(true);		
	}

	public static void main(String[] args) throws ParserConfigurationException, TransformerException, SAXException, IOException {
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				createAndShowGUI();
			}

			
		});
		
	}
	
	public static void Save(String name, Document bpel, Document wsdl, Document deploy, String filePath) throws ParserConfigurationException, TransformerException {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        
        DOMSource source = new DOMSource(bpel);
        StreamResult streamResult =  new StreamResult(new File(filePath) + "/" + name + ".bpel");
        transformer.transform(source, streamResult);
        
        source = new DOMSource(wsdl);
        streamResult =  new StreamResult(new File(filePath) + "/" + name + ".wsdl");
        transformer.transform(source, streamResult);
        
        source = new DOMSource(deploy);
        streamResult =  new StreamResult(new File(filePath) + "/deploy.xml");
        transformer.transform(source, streamResult);

	}

}
